#include "stdafx.h"
#include <JeayeSDK/j_md5.h> 

#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21

/* LR rotates x left n bits*/
#define md5_LR(X,N)        ((X) << (N))|((X) >> (32 - N))
/* F,G,H and I are basic MD5 functions.*/
#define md5_F(X,Y,Z)    (((X) & (Y)) | ((~X) & (Z)))
#define md5_G(X,Y,Z)    (((X) & (Z)) | ((Y) & (~Z)))
#define md5_H(X,Y,Z)    ((X) ^ (Y) ^ (Z))
#define md5_I(X,Y,Z)    ((Y) ^ ((X) | (~Z)))

#define md5_LR_A(a,b,y)                                        \
{                                                            \
    (a) = md5_LR((a),(y));                                    \
    (a) += (b);                                                \
}                                                            
/* FF,GG,HH,AND II transformations for rounds 1,2,3 and 4*/
#define md5_FF(a,b,c,d,x,y,z)                                \
{                                                            \
    (a) += md5_F((b),(c),(d))+(x)+(UINT32)(z);                \
    md5_LR_A((a),(b),(y))                                    \
}                                                            

#define md5_GG(a,b,c,d,x,y,z)                                \
{                                                            \
    (a) += md5_G((b),(c),(d))+(x)+(UINT32)(z);                \
    md5_LR_A((a),(b),(y))                                    \
}                                                            

#define md5_HH(a,b,c,d,x,y,z)                                \
{                                                            \
    (a) += md5_H((b),(c),(d))+(x)+(UINT32)(z);                \
    md5_LR_A((a),(b),(y))                                    \
}                                                            

#define md5_II(a,b,c,d,x,y,z)                                \
{                                                            \
    (a) += md5_I((b),(c),(d))+(x)+(UINT32)(z);                \
    md5_LR_A((a),(b),(y))                                    \
}                                                            

const BYTE MD5_PADDING[64] = {
	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0,0, 0, 0, 0, 0, 0, 0
};
const UINT32 MD5_STATE[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
/**/
void  md5_transform(UINT32 state[4], const BYTE block[64]);
void  md5_encode(const UINT32 *, BYTE *, UINT32);
void  md5_decode(const BYTE *, UINT32 *, UINT32);

/**/
void  c_md5_init(c_md5_t *thiz)
{
	/*clean*/
	memset((void*)thiz, 0, sizeof(c_md5_t));
	/**/
	thiz->state[0] = MD5_STATE[0];
	thiz->state[1] = MD5_STATE[1];
	thiz->state[2] = MD5_STATE[2];
	thiz->state[3] = MD5_STATE[3];
}

/**/
void  c_md5_update(c_md5_t *thiz, const BYTE *input, UINT32 length)
{
	UINT32 i, index, partLen;
	/* Compute number of unsigned chars mod 64 */
	index = (UINT32)((thiz->count[0] >> 3) & 0x3f);

	/* update number of bits */
	if ((thiz->count[0] += ((UINT32)length << 3)) < ((UINT32)length << 3)) {
		thiz->count[1]++;
	}
	thiz->count[1] += ((UINT32)length >> 29);

	partLen = 64 - index;

	/* transform as many times as possible. */
	if (length >= partLen) {
		memcpy((void*)&thiz->buffer[index], input, partLen);
		md5_transform(thiz->state, thiz->buffer);

		for (i = partLen; i + 63 < length; i += 64) {
			md5_transform(thiz->state, &input[i]);
		}
		index = 0;
	}
	else {
		i = 0;
	}
	/* Buffer remaining input */
	memcpy((void *)&thiz->buffer[index], &input[i], length - i);
}

/**/
void  c_md5_finalize(c_md5_t *thiz)
{
	BYTE bits[8];
	UINT32 index, padLen;

	/* Save number of bits */
	md5_encode(thiz->count, bits, 8);

	/* Pad out to 56 mod 64. */
	index = ((thiz->count[0] >> 3) & 0x3f);
	padLen = (index < 56) ? (56 - index) : (120 - index);
	c_md5_update(thiz, MD5_PADDING, padLen);

	/* Append length (before padding) */
	c_md5_update(thiz, bits, 8);

	/* Store state in digest */
	md5_encode(thiz->state, thiz->digest, 16);
}

/**/
void  md5_transform(UINT32 state[4], const BYTE block[64])
{
	UINT32 x[16], a = state[0], b = state[1], c = state[2], d = state[3];

	md5_decode(block, x, 64);

	md5_FF(a, b, c, d, x[0], S11, 0xd76aa478);
	md5_FF(d, a, b, c, x[1], S12, 0xe8c7b756);
	md5_FF(c, d, a, b, x[2], S13, 0x242070db);
	md5_FF(b, c, d, a, x[3], S14, 0xc1bdceee);
	md5_FF(a, b, c, d, x[4], S11, 0xf57c0faf);
	md5_FF(d, a, b, c, x[5], S12, 0x4787c62a);
	md5_FF(c, d, a, b, x[6], S13, 0xa8304613);
	md5_FF(b, c, d, a, x[7], S14, 0xfd469501);
	md5_FF(a, b, c, d, x[8], S11, 0x698098d8);
	md5_FF(d, a, b, c, x[9], S12, 0x8b44f7af);
	md5_FF(c, d, a, b, x[10], S13, 0xffff5bb1);
	md5_FF(b, c, d, a, x[11], S14, 0x895cd7be);
	md5_FF(a, b, c, d, x[12], S11, 0x6b901122);
	md5_FF(d, a, b, c, x[13], S12, 0xfd987193);
	md5_FF(c, d, a, b, x[14], S13, 0xa679438e);
	md5_FF(b, c, d, a, x[15], S14, 0x49b40821);

	md5_GG(a, b, c, d, x[1], S21, 0xf61e2562);
	md5_GG(d, a, b, c, x[6], S22, 0xc040b340);
	md5_GG(c, d, a, b, x[11], S23, 0x265e5a51);
	md5_GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);
	md5_GG(a, b, c, d, x[5], S21, 0xd62f105d);
	md5_GG(d, a, b, c, x[10], S22, 0x2441453);
	md5_GG(c, d, a, b, x[15], S23, 0xd8a1e681);
	md5_GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);
	md5_GG(a, b, c, d, x[9], S21, 0x21e1cde6);
	md5_GG(d, a, b, c, x[14], S22, 0xc33707d6);
	md5_GG(c, d, a, b, x[3], S23, 0xf4d50d87);
	md5_GG(b, c, d, a, x[8], S24, 0x455a14ed);
	md5_GG(a, b, c, d, x[13], S21, 0xa9e3e905);
	md5_GG(d, a, b, c, x[2], S22, 0xfcefa3f8);
	md5_GG(c, d, a, b, x[7], S23, 0x676f02d9);
	md5_GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);

	md5_HH(a, b, c, d, x[5], S31, 0xfffa3942);
	md5_HH(d, a, b, c, x[8], S32, 0x8771f681);
	md5_HH(c, d, a, b, x[11], S33, 0x6d9d6122);
	md5_HH(b, c, d, a, x[14], S34, 0xfde5380c);
	md5_HH(a, b, c, d, x[1], S31, 0xa4beea44);
	md5_HH(d, a, b, c, x[4], S32, 0x4bdecfa9);
	md5_HH(c, d, a, b, x[7], S33, 0xf6bb4b60);
	md5_HH(b, c, d, a, x[10], S34, 0xbebfbc70);
	md5_HH(a, b, c, d, x[13], S31, 0x289b7ec6);
	md5_HH(d, a, b, c, x[0], S32, 0xeaa127fa);
	md5_HH(c, d, a, b, x[3], S33, 0xd4ef3085);
	md5_HH(b, c, d, a, x[6], S34, 0x4881d05);
	md5_HH(a, b, c, d, x[9], S31, 0xd9d4d039);
	md5_HH(d, a, b, c, x[12], S32, 0xe6db99e5);
	md5_HH(c, d, a, b, x[15], S33, 0x1fa27cf8);
	md5_HH(b, c, d, a, x[2], S34, 0xc4ac5665);

	md5_II(a, b, c, d, x[0], S41, 0xf4292244);
	md5_II(d, a, b, c, x[7], S42, 0x432aff97);
	md5_II(c, d, a, b, x[14], S43, 0xab9423a7);
	md5_II(b, c, d, a, x[5], S44, 0xfc93a039);
	md5_II(a, b, c, d, x[12], S41, 0x655b59c3);
	md5_II(d, a, b, c, x[3], S42, 0x8f0ccc92);
	md5_II(c, d, a, b, x[10], S43, 0xffeff47d);
	md5_II(b, c, d, a, x[1], S44, 0x85845dd1);
	md5_II(a, b, c, d, x[8], S41, 0x6fa87e4f);
	md5_II(d, a, b, c, x[15], S42, 0xfe2ce6e0);
	md5_II(c, d, a, b, x[6], S43, 0xa3014314);
	md5_II(b, c, d, a, x[13], S44, 0x4e0811a1);
	md5_II(a, b, c, d, x[4], S41, 0xf7537e82);
	md5_II(d, a, b, c, x[11], S42, 0xbd3af235);
	md5_II(c, d, a, b, x[2], S43, 0x2ad7d2bb);
	md5_II(b, c, d, a, x[9], S44, 0xeb86d391);

	state[0] += a;
	state[1] += b;
	state[2] += c;
	state[3] += d;
	/* Zeroize sensitive information.*/
	memset((void *)x, 0, sizeof(x));
}

/* Encodes input (UINT32) into output (unsigned char). Assumes length is a multiple of 4.*/
void  md5_encode(const UINT32 *input, BYTE *output, UINT32 length)
{
	UINT32 i = 0, j = 0;
	for (; j < length; i++, j += 4) {
		output[j] = (BYTE)(input[i] & 0xff);
		output[j + 1] = (BYTE)((input[i] >> 8) & 0xff);
		output[j + 2] = (BYTE)((input[i] >> 16) & 0xff);
		output[j + 3] = (BYTE)((input[i] >> 24) & 0xff);
	}
}

/* Decodes input (unsigned char) into output (UINT32). Assumes length is a multiple of 4.*/
void  md5_decode(const BYTE *input, UINT32 *output, UINT32 length)
{
	UINT32 i = 0, j = 0;
	for (; j < length; i++, j += 4) {
		output[i] = ((UINT32)input[j]) | (((UINT32)input[j + 1]) << 8) |
			(((UINT32)input[j + 2]) << 16) | (((UINT32)input[j + 3]) << 24);
	}
}

/*
Digests a file.
*/
BOOL  c_md5file(const TCHAR *filename, TCHAR *results)
{
	FILE *file;
	c_md5_t thiz;
	BYTE t = 0;
	INT32 len = 0, i = 0, index = 0;
	BYTE buffer[1024];

	if ((file = _tfopen(filename, _T("rb"))) == NULL)
		return FALSE;
	else {
		c_md5_init(&thiz);
		while ((len = (INT32)fread(buffer, sizeof(BYTE), _countof(buffer), file)))
			c_md5_update(&thiz, buffer, len);
		c_md5_finalize(&thiz);

		fclose(file);

		if (results) {
			memset(results, 0, sizeof(16));
			for (i = 0; i < 16; i++) {
				t = thiz.digest[i];
				results[index++] = Value2T(t / 16);
				results[index++] = Value2T(t % 16);
			}
			results[index] = _T('\0');
		}
	}
	return TRUE;
}

/**/
const wchar_t*  c_md5(const BYTE *input, UINT32 length, wchar_t *results)
{
	BYTE t = 0;
	INT32 i = 0, index = 0;
	c_md5_t thiz;

	c_md5_init(&thiz);
	c_md5_update(&thiz, input, length);
	c_md5_finalize(&thiz);

	if (results) {
		for (i = 0; i < 16; i++) {
			t = thiz.digest[i];
			results[index++] = Value2W(t / 16);
			results[index++] = Value2W(t % 16);
		}
		results[index] = 0;
	}
	return results;
}

const char * c_md5(const BYTE *input, UINT32 length, char *results)
{
	BYTE t = 0;
	INT32 i = 0, index = 0;
	c_md5_t thiz;

	c_md5_init(&thiz);
	c_md5_update(&thiz, input, length);
	c_md5_finalize(&thiz);

	if (results) {
		for (i = 0; i < 16; i++) {
			t = thiz.digest[i];
			results[index++] = Value2A(t / 16);
			results[index++] = Value2A(t % 16);
		}
		results[index] = 0;
	}
	return results;
}
